﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Web;
using System.Linq;

namespace Component
{
    public class ImageDB
    {
        static Catching catching = new Catching();
        static string pathFileXML = HttpContext.Current.Server.MapPath(Config.GetPathXMLOfFileName(PathFileNameXml._Images));
        public static List<ImageInfo> GetAll()
        {
            List<ImageInfo> result = new List<ImageInfo>();
            result = catching.GetObject(StaticCatchingKey._Images_List) as List<ImageInfo>;
            if (result != null)
                return result;
            else
            {
                result = ProviderXML.GetAll_Images(pathFileXML);
                catching.PubObject(StaticCatchingKey._Images_List, result, Config.CatchTimeSpan);
                return result;
            }
        }
        public static ImageInfo GetById(int id)
        {
            List<ImageInfo> list = new List<ImageInfo>();
            ImageInfo info = new ImageInfo();
            list = catching.GetObject(StaticCatchingKey._Images_List) as List<ImageInfo>;
            if (list != null)
                return list.Single(c => c.Id == id);
            else
            {
                list = ProviderXML.GetAll_Images(pathFileXML);
                info = list.Single(c => c.Id == id);
                catching.PubObject(StaticCatchingKey._Images_List, list, Config.CatchTimeSpan);
                return info;
            }
        }

        public static bool Insert(ImageInfo info)
        {
            catching.RemoveObject(StaticCatchingKey._Images_List);
            return ProviderXML.Insert_Images(info, pathFileXML);
        }
        public static bool Delete(int id)
        {
            catching.RemoveObject(StaticCatchingKey._Images_List);
            return ProviderXML.Detele(id, pathFileXML);
        }
    }
}
